/**
 * FavoriteBooks.java
 *
 * Copyright (c) 2011 Alexandr Tsvetkov (E-mail: al@ukr.net)
 *
 * License agreement:
 *
 * 1. This code is published AS IS. Author is not responsible for any damage that can be
 *    caused by any application that uses this code.
 * 2. Author does not give a garantee, that this code is error free.
 * 3. This code can be used in NON-COMMERCIAL applications AS IS without any special
 *    permission from author.
 * 4. This code can be modified without any special permission from author IF AND ONLY IF
 *    this license agreement will remain unchanged.
 * 5. SPECIAL PERMISSION for this code usage in COMMERCIAL application SHOULD be obtained
 *    from author.
 */
package ua.at.tsvetkov.samizdat.data;

import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.data.StoreString;
import ua.at.tsvetkov.util.Log;

/**
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class ReadedBooksData extends StoreString {

	public ReadedBooksData() {
		super(Const.FILE_READED_BOOKS_DATA);
	}

	public Book getBookData(Book book) {
		String object = book.object;
		for (String bookStr : data) {
			if (bookStr.contains(object)) {
				try {
					book.init(bookStr);
				} catch (WrongFieldCountException e) {
					Log.w("Ошибка получения данных о книге");
				}
				break;
			}
		}
		return book;
	}

	public void putBook(Book book) {
		String object = book.object;
		String old = "";
		for (String bookStr : data) {
			if (bookStr.contains(object)) {
				old = bookStr;
				break;
			}
		}
		data.remove(old);
		data.add(book.getFormattedString());
	}

	public void removeBook(Book book) {
		String url = book.getUrl();
		String old = "";
		for (String bookStr : data) {
			if (bookStr.contains(url)) {
				old = bookStr;
				break;
			}
		}
		data.remove(old);
	}

}
